#include<vector>
#include<algorithm>
#include<iostream>
using namespace std;

class Solution {
public:
    void nextPermutation(vector<int>& nums) {
        int n = nums.size();
        bool flag = false;
        int ii = 0, jj = 0;

        // 从后往前找到第一个正序对
        for (int j = n - 1; j >= 0; j--) {
            for (int i = j - 1; i >= 0; i--){
                if (nums[i] < nums[j]) {
                    flag = true;

                    jj = j;
                    ii = i;
                    cout << ii << " " << jj << endl;
                    break;
                }
            }
            if (flag==true) break;
        }
        if (flag==false) {
            sort(nums.begin(), nums.end());
        }else{
            swap(nums[ii],nums[jj]);
            sort(nums.begin() + ii + 1, nums.end());
        }
    }
};